package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import com.itheima.reggie.service.SetmealDishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐，同时需要保存套餐和菜品的关联关系
     *
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐基本信息
        this.save(setmealDto);
        Long setmealId = setmealDto.getId();
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        setmealDishList = setmealDishList.stream().map((item) -> {
            item.setSetmealId(setmealId);
            return item;
        }).collect(Collectors.toList());
        //保存套餐和菜品的关联信息
        setmealDishService.saveBatch(setmealDishList);
    }

    /**
     * 根据id查询套餐和对应的信息
     *
     * @param id
     * @return
     */
    public SetmealDto getByIdWithSetmealDish(Long id) {
        //查询setmeal表
        Setmeal setmeal = this.getById(id);

        SetmealDto dto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, dto);

        //查询setmeal_dish
        Long setmealId = setmeal.getId();
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(setmealId != null, SetmealDish::getSetmealId, setmealId);
        lqw.orderByDesc(SetmealDish::getUpdateTime);
        List<SetmealDish> list = setmealDishService.list(lqw);

        dto.setSetmealDishes(list);

        return dto;
    }

    /**
     * 更新套餐，同时更新套餐对应的数据，需要操作两张表
     *
     * @param setmealDto
     * @return
     */
    @Override
    @Transactional
    public void updateWithSetmealDish(SetmealDto setmealDto) {
        //更新setmeal表基本信息
        this.updateById(setmealDto);

        //清理setmeal_dish当前对应数据
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(lqw);

        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        Long sid = setmealDto.getId();
        setmealDishList = setmealDishList.stream().map((item) -> {
            item.setSetmealId(sid);
            return item;
        }).collect(Collectors.toList());

        //添加当前提交的口味数据
        setmealDishService.saveBatch(setmealDishList);

    }

    /**
     * 删除套餐，同时更新对应的套餐数据
     *
     * @param ids
     */
    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        //select count(*) from table where id in(1,2,3) and status=1
        //查询套餐状态 若为起售则不能删除
        LambdaQueryWrapper<Setmeal> lqws = new LambdaQueryWrapper<>();
        lqws.in(Setmeal::getId, ids);
        lqws.eq(Setmeal::getStatus, 1);
        int count = this.count(lqws);
        if (count > 0) {
            throw new CustomException("套餐起售中，不能删除");
        }
        //可以删除
        this.removeByIds(ids);

        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();

        lqw.in(SetmealDish::getSetmealId, ids);

        setmealDishService.remove(lqw);


    }


}
